package com.fyzk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.fyzk.activity.dialog.SeniorZhuiHaoActivity;
import com.fyzk.view.ASView;
import com.fyzk.bean.SeniorPhaseItem;
import com.tyjc.lottery.R;

import java.util.List;

public class PhaseListAdapter extends BaseAdapter {

    private List<SeniorPhaseItem> seniorPhaseItems;
    private Context context;


    public PhaseListAdapter(SeniorZhuiHaoActivity context, List<SeniorPhaseItem> seniorPhaseItems) {
        this.context = context;
        this.seniorPhaseItems = seniorPhaseItems;
    }

    @Override
    public int getCount() {
        return this.seniorPhaseItems.size();
    }

    @Override
    public SeniorPhaseItem getItem(int position) {
        return this.seniorPhaseItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View itemView, ViewGroup parent) {

        final int index = position;

        ViewHolder holder = null;

        if (itemView == null) {
            itemView = LayoutInflater.from(context).inflate(R.layout
                            .senior_zhuihao_item,
                    null);
            holder = new ViewHolder();
            holder.touBei = (ASView) itemView
                    .findViewById(R.id.seniorzhuihao_item_beishu);
            holder.phase_tv = (TextView) itemView.findViewById(R.id.seniorzhuihao_item_phase);
            holder.check_iv = (CheckBox) itemView.findViewById(R.id
                    .seniorzhuihao_item_check_iv);
            holder.seniorzhuihao_item_amount = (TextView) itemView.findViewById(R.id
                    .seniorzhuihao_item_amount);
            itemView.setTag(holder);
        } else {
            holder = (ViewHolder) itemView.getTag();
        }
        SeniorPhaseItem seniorPhaseItem = seniorPhaseItems.get(index);
        holder.phase_tv.setText(seniorPhaseItem.phase);
        holder.touBei.setValue(seniorPhaseItem.multiple);
        holder.check_iv.setOnCheckedChangeListener(null);
        holder.check_iv.setChecked(seniorPhaseItem.isSel);
        holder.check_iv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                seniorPhaseItems.get(index).isSel = isChecked;
                PhaseListAdapter.this.notifyDataSetChanged();
            }
        });
        holder.touBei.setOnValueChangedLisener(new ASView.OnValueChangedLisener() {
            @Override
            public void onValueChanged(ASView view, int value) {
                seniorPhaseItems.get(index).multiple = value;
                PhaseListAdapter.this.notifyDataSetChanged();
            }
        });

        holder.seniorzhuihao_item_amount.setText(holder.touBei.getValue() * seniorPhaseItem.amt +
                "米");


        return itemView;
    }


    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();

    }


    class ViewHolder {
        TextView phase_tv;
        TextView seniorzhuihao_item_amount;
        ASView touBei;
        CheckBox check_iv;
    }
}